package io.xxx.omni.sync

import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import io.xxx.omni.util.JSONTypeHandler
import io.xxx.omni.util.toLocalDateTime
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Param
import org.springframework.stereotype.Repository
import java.time.LocalDateTime

/**
 * 平台
 */
data class Platform(
    @TableId
    var code: String?,
    var briefCode: String?,
    var name: String?,
    var enabled: Boolean?,
    var seqNo: Int?,
    var created: LocalDateTime?,
    var modified: LocalDateTime?,
)

@Mapper
@Repository
interface PlatformMapper : BaseMapper<Platform>

/**
 * 店铺
 */
data class Store(
    @TableId(type = IdType.AUTO)
    var code: String?,
    var name: String?,
    var platformCode: String?,
    var enabled: Boolean?,
    var appKey: String?,
    var appSecret: String?,
    var accessToken: String?,
    var created: LocalDateTime?,
    var modified: LocalDateTime?,
    @TableField(exist = false) var platform: Platform?
) {
    @Suppress("unused")
    constructor() : this(null, null, null, null, null, null, null, null, null, null)
}

@Mapper
@Repository
interface StoreMapper : BaseMapper<Store>

///**
// * 同步任务最后的调度信息
// */
//data class Schedule(
//    @TableId(type = IdType.AUTO)
//    var id: Int?,
//    var platformCode: String?,
//    var storeCode: String?,
//    var jobId: Int?,
//    var endTime: LocalDateTime?
//) {
//    constructor(platformCode: String?, storeCode: String?, jobId: Int?, endTime: LocalDateTime?)
//            : this(null, platformCode, storeCode, jobId, endTime)
//}
//
//@Mapper
//@Repository
//interface ScheduleMapper : BaseMapper<Schedule>

data class Document(
    @TableId(type = IdType.AUTO)
    var id: Long?,
    var platformCode: String?,
    var storeCode: String?,
    var jobId: Int?,
    var sn: String?,
    var rsn: String?,
    var data: JSONObject?,
    var modified: LocalDateTime?,
    var syncCreated: LocalDateTime?,
    var syncModified: LocalDateTime?
) {
    @Suppress("unused")
    constructor() : this(null, null, null, null, null, null, null, null, null, null)

    constructor(sn: String?, data: JSONObject?, modified: LocalDateTime?)
            : this(null, null, null, null, sn, null, data, modified, null, null)

    constructor(sn: String?, rsn: String?, data: JSONObject?, modified: LocalDateTime?)
            : this(null, null, null, null, sn, rsn, data, modified, null, null)
}

@Mapper
@Repository
interface DocumentMapper : BaseMapper<Document> {

    fun insertAll(@Param("list") documents: List<Document>)

    fun updateAll(@Param("list") documents: List<Document>)
}

data class AkcActivity(
    @TableId
    var id: String?,
    var name: String?,
    var storeCode: String?,
    var beginTime: LocalDateTime?,
    var endTime: LocalDateTime?,
    var completed: Boolean?,
    @TableField(typeHandler = JSONTypeHandler::class)
    var data: JSONObject?
) {
    @Suppress("unused")
    constructor() : this(null, null, null, null, null, null, null)

    constructor(store: Store, jsonObject: JSONObject) : this(
        jsonObject.getString("id"),
        jsonObject.getString("name"),
        store.code,
        jsonObject.getString("begintime").toLocalDateTime(),
        jsonObject.getString("endtime").toLocalDateTime(),
        false,
        jsonObject
    )

    override fun equals(other: Any?): Boolean {
        if (other is AkcActivity) {
            return other.id == id && other.beginTime == beginTime && other.endTime == endTime
        }
        return false
    }

    override fun hashCode(): Int {
        var result = id?.hashCode() ?: 0
        result = 31 * result + (beginTime?.hashCode() ?: 0)
        result = 31 * result + (endTime?.hashCode() ?: 0)
        return result
    }
}

@Mapper
@Repository
interface AkcActivityMapper : BaseMapper<AkcActivity>

/**
 * 任务信息
 */
data class Job(
    @TableId(type = IdType.AUTO)
    var id: Int?,
    var name: String?,
    var enabled: Boolean?,
    var created: LocalDateTime?,
    var modified: LocalDateTime?
)

@Mapper
@Repository
interface JobMapper : BaseMapper<Job>

/**
 * 店铺任务信息
 */
data class StoreJob(
    @TableId(type = IdType.AUTO)
    var id: Int?,
    var platformCode: String?,
    var storeCode: String?,
    var jobId: Int?,
    var enabled: Boolean?,
    /**
     * 任务最后一次执行的时间
     */
    var endTime: LocalDateTime?,
)

@Mapper
@Repository
interface StoreJobMapper : BaseMapper<StoreJob>